package vmcs.boundary;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto
//
import vmcs.controller.StoreController;
import vmcs.boundary.listener.StoreViewerListener;
import java.awt.*;
import vmcs.entity.StoreItem;
import vmcs.entity.StoreObject;
import vmcs.utility.VMCSException;

public class StoreViewer extends Panel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private LabelledDisplay viewItems[];
    private StoreController storeCtrl;
    private char type;
    public static final String CASH_VIEW_TITLE = "Quantity of Coins Available";
    public static final String DRINK_VIEW_TITLE = "Quantity of Drinks Available";
    private int sSize;  // store size;

    public StoreViewer(char ti, StoreController sctrl) {
        storeCtrl = sctrl;

        Label lb;

        type = ti;
        if (type == StoreController.CASH) {
            lb = new Label(CASH_VIEW_TITLE);
        } else {
            lb = new Label(DRINK_VIEW_TITLE);

        }
        lb.setFont(new Font("Helvetica", Font.BOLD, 12));

        sSize = storeCtrl.getStoreSize(type);

        Panel pl = new Panel(new FlowLayout(FlowLayout.LEFT));
        pl.add(lb);

        viewItems = new LabelledDisplay[sSize];

        StoreItem[] storeItem;
        storeItem = storeCtrl.getStoreItems(type);

        this.setLayout(new GridLayout(0, 1));

        this.add(pl);

        String name;
        String sval;
        int val;
        int i;

        StoreViewerListener svl;
        StoreObject ob;

        for (i = 0; i < sSize; i++) {
            ob = storeItem[i].getContent();
            name = ob.getName();
            val = storeItem[i].getQuantity();
            sval = String.valueOf(val);
            viewItems[i] = new LabelledDisplay(name, LabelledDisplay.DEFAULT, LabelledDisplay.GRID);
            viewItems[i].setValue(sval);
            svl = new StoreViewerListener(type, i, storeCtrl);
            viewItems[i].addListener(svl);
            this.add(viewItems[i]);
        }
    }

    public void update(int idx, int qty)
            throws VMCSException {
        if (idx >= sSize) {
            throw new VMCSException("StoreViewer.update", "index overflow");
        }
        viewItems[idx].setValue(qty);
    }

    public void closeDown() {
    }

    public void setActive(boolean state) {
        this.setEnabled(state);
    }
}
